package com.gs.SynPatrolRep;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.multidex.MultiDex;

import com.gs.SynPatrolRep.activity.LoginActivity;
import com.gs.SynPatrolRep.activity.MainActivity;
import com.gs.SynPatrolRep.constant.AllConstants;
import com.gs.SynPatrolRep.util.SPUtil;
import com.uuzuche.lib_zxing.activity.ZXingLibrary;

public class MyApplication extends Application {
    private static Context context;
    private static int mCount;
    private static boolean mFront; // 是否前台

    @Override
    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();
        registerActivityLifecycleCallbacks();
        MultiDex.install(this);
        ZXingLibrary.initDisplayOpinion(this);

    }

    public static Context getContext() {
        return context;
    }

    public static void startLogin() {
        Intent intent = new Intent(context, LoginActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        context.startActivity(intent);
    }

    private void registerActivityLifecycleCallbacks() {
        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) {

            }

            @Override
            public void onActivityStarted(@NonNull Activity activity) {
                mCount++;
                if (!mFront) {
                    mFront = true;
                    if (!TextUtils.isEmpty(SPUtil.getInstance().getString(AllConstants.TOKEN))) {
                        Log.i(AllConstants.TAG, "AppContext------>处于前台");
//                        Intent intent = new Intent(activity, SignalRService.class);
//                        stopService(intent);
//                        startService(intent);
                    }

                }
            }

            @Override
            public void onActivityResumed(@NonNull Activity activity) {

            }

            @Override
            public void onActivityPaused(@NonNull Activity activity) {

            }

            @Override
            public void onActivityStopped(@NonNull Activity activity) {
                mCount--;
                if (mCount == 0) {
                    mFront = false;
                    Log.i(AllConstants.TAG, "AppContext------>处于后台");
                }
            }

            @Override
            public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle outState) {

            }

            @Override
            public void onActivityDestroyed(@NonNull Activity activity) {
                if (activity instanceof MainActivity) {

                }

            }
        });
    }
}
